<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace plugins\dashboard\controller; //Demo插件英文名，改成你的插件英文就行了
use cmf\controller\PluginBaseController;
use think\Db;

class IndexController extends PluginBaseController
{

    function index()
    {
        $type = isset($_POST['date']) ? $_POST['date'] : 4;
        $data = array();
        if ($type == 1) {
            $today=[
                mktime(0, 0, 0, date('m'), date('d'), date('Y')),
                mktime(23, 59, 59, date('m'), date('d'), date('Y'))
            ];
            for ($i = 0; $i < 24; $i ++) {
                $start=$today[0];
                $time[0] = $start + 3600 * $i;
                $time[1] = $start + 3600 * ($i + 1);
                $where['order_status']=['egt',1];
                $where['create_time']=['between',$time];
                $count=Db::name('order')->where($where)->count();
                $data[$i] = array(
                    $i . ':00',
                    (int)$count
                );
            }
        } elseif ($type == 2) {
            $yesterday = date('d') - 1;
            $yesterday=[
                mktime(0, 0, 0, date('m'), $yesterday, date('Y')),
                mktime(23, 59, 59, date('m'), $yesterday, date('Y'))
            ];;
            $start=$yesterday[0];
            for ($j = 0; $j < 24; $j ++) {
                $time[0] = $start + 3600 * $j;
                $time[1] =$start + 3600 * ($j + 1);
                $where['order_status']=['egt',1];
                $where['create_time']=['between',$time];
                $count=Db::name('order')->where($where)->count();
                $data[$j] = array(
                    $j . ':00',
                    (int)$count
                );
            }
        } elseif ($type == 3) {
            $timestamp = time();
            $week=[
                strtotime(date('Y-m-d', strtotime("+0 week Monday", $timestamp))),
                strtotime(date('Y-m-d', strtotime("+0 week Sunday", $timestamp))) + 24 * 3600 - 1
            ];
            $start=$week[0];
            $start = $start - 604800;
            for ($j = 0; $j < 7; $j ++) {
                $time[0] = $start + 86400 * $j;
                $time[1] = $start + 86400 * ($j + 1);
                $where['order_status']=['egt',1];
                $where['create_time']=['between',$time];
                $count=Db::name('order')->where($where)->count();
                $data[$j] = array(
                    '星期' . ($j + 1),
                    (int)$count
                );
            }
        } elseif ($type == 4) {
            $month=[
                mktime(0, 0, 0, date('m'), 1, date('Y')),
                mktime(23, 59, 59, date('m'), date('t'), date('Y'))
            ];
            $start=$month[0];
            $end=$month[1];
            for ($j = 0; $j < ($end + 1 - $start) / 86400; $j ++) {
                $time[0] =$start + 86400 * $j;
                $time[1] = $start + 86400 * ($j + 1);
                $where['order_status']=['egt',1];
                $where['create_time']=['between',$time];
                $count=Db::name('order')->where($where)->count();
                $data[$j] = array(
                    (1 + $j) . '日',
                    (int)$count
                );
            }
        }
        return $data;
    }

}
